<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_creditchange/model')->方法名();
*/
namespace jy_creditchange;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function checkvip(){
		$user=pdo_get('jy_creditchange_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));		
		if(!$user['isvip']){
				exi('只有VIP会员才能操作！','error','/home/');
		}elseif($user['viptime']<time()){
				exi('您的VIP已过期，无权操作！','error','/home/');
		}
		return true;
	}
	public function checkorders($goodsid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$orders=pdo_getall('jy_creditchange_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'','','fee desc');
		return $orders;
	}
	public function dacheng($orderid){
		$order=pdo_get('jy_creditchange_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['status']!=1){
			exi('订单状态不符合条件，不能成交！','error');
		}
		pdo_update('jy_creditchange_order',array('status'=>2),array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));		
		$goods=pdo_get('jy_creditchange_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['goodsid']));
		//竞价方获得积分
		model('core/member')->member_credit($order['mid'],'credit'.$goods['type'],$goods['creditnum'],true,'jy_creditchange','竞拍成功');
		//出让方解冻消耗
		$this->model('member')->recharge($goods['mid'],$goods['creditnum'],$goods['type'],1);
		//出让方获得现金红包
		$red=array(
			'mid'=>$goods['mid'],
			'pidcode'=>$goods['id'],
			'plugin'=>'jy_creditchange',
			'money'=>$order['fee'],
			'remark'=>'出让虚拟币获得现金红包',
		);
		model('core/red_pack')->get_money($red);
		pdo_update('jy_creditchange_goods',array('status'=>2),array('id'=>$order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$allorders=pdo_getall('jy_creditchange_order',array('status'=>1,'ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'goodsid'=>$order['goodsid']));
		foreach($allorders as $ov){
			if($ov['id']!=$orderid){
				pdo_update('jy_creditchange_order',array('status'=>3),array('id'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']));
				$ov['status']=3;
				$this->model('member')->tuikuan($ov,true);
			}
		}
		return true;
	}
	public function setgou($goodsid){
		$goods=pdo_get('jy_creditchange_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
		if($goods['status']!=1){
			exi('商品状态不符合条件，不能回购！','error');
		}
		$basic=$this->plugin_setting('basic');
		if(!$basic['is_gou']){
			exi('系统不支持回购！','error');
		}
		if(!$basic['gourate'.$goods['type']]){
			$title=credit_types()[$goods['type']];
			exi($title.'回购比例未设置！请联系客服！','error');
		}
		pdo_update('jy_creditchange_goods',array('status'=>5),array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		$fee=intval($goods['creditnum']*$basic['gourate'.$goods['type']])/100;
		//回购
		$this->model('member')->recharge($goods['mid'],$goods['creditnum'],$goods['type'],3);
		//出让方获得现金红包
		$red=array(
			'mid'=>$goods['mid'],
			'pidcode'=>$goods['id'],
			'plugin'=>'jy_creditchange',
			'money'=>$fee,
			'remark'=>'回购虚拟币获得现金红包',
		);
		model('core/red_pack')->get_money($red);
		$allorders=pdo_getall('jy_creditchange_order',array('status'=>1,'ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid));
		foreach($allorders as $ov){
			pdo_update('jy_creditchange_order',array('status'=>3),array('id'=>$ov['id'],'uniacid'=>$_SESSION['uniacid']));
			$ov['status']=3;
			$this->model('member')->tuikuan($ov,true);
		}
		return true;
	}
}
?>